
/**
 * 鼠标右键事件（添加节点，移除节点功能）
 * @param { import('konva').default.Stage } stage
 * @param { import('./index').FlowChart } instance
 */
export const contextmenuEvt = (stage, instance) => {
  const container = instance.stage.container()
  stage.on('contextmenu', (e) => {
    let menu = []
    e.evt.preventDefault()
    const { offsetX, offsetY } = e.evt
    const div = document.createElement('div')
    div.className = 'contextmenu_kyjscc66'
    div.style.position = 'absolute'
    div.style.left = offsetX + 'px'
    div.style.top = offsetY + 'px'
    const name = e.target.attrs.name
    if (name === 'stage') {
      console.log('a')
      menu = instance.menu.filter(i => i.name === '添加一级节点')
    } else if (name === 'lifeLine') {
      menu = instance.menu
    } else if (name === 'childNode') {
      menu = instance.menu.filter(i => i.name === '删除当前节点')
    }

    menu.forEach((item) => {
      const div2 = document.createElement('div')
      div2.classList.add('contextmenu-item')
      div2.innerText = item.name
      div.appendChild(div2)
      div2.onclick = () => {
        item.add(e)
      }
    })

    instance.contextmenuDom && container.removeChild(instance.contextmenuDom)
    instance.contextmenuDom = div
    container.appendChild(div)
  })
  stage.on('click', (e) => {
    instance.closeContextMenu()
    if (e.target.getType() === 'Stage') {
      instance.stage.find('.activeNode').forEach((node) => {
        node.destroy()
      })
      instance.setActiveNode(null)
    }
  })
}

